package com.itheima.test;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author by itheima
 * @Date 2021/8/20
 * @Description
 */
public class MybatisTest2 {

    /**
     * 开发中推荐使用基于接口动态代理的方式
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //1.将核心配置文件转换成文件流对象
        //说明：Resources是mybatis给我们提供的加载配置文件的工具类：org.apache.ibatis.io.Resources
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.构建会话工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        //3.从会话工厂获取会话对象
        //说明：会话对象就是操纵sql进行增删改查的核心对象 底层对Connection封装
        SqlSession session = sessionFactory.openSession();
        //4.获取接口的代理对象
        //说明：传入接口的字节码对象，获取接口的动态代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5.调用接口方法，完成功能
        User user = mapper.findUserById(1);
        System.out.println(user);
        //5.关闭资源，释放连接
        session.close();
        in.close();
    }

    
    
    
}
